var app = getApp()
var http = app.require('utils/http.js')
var util = app.require('utils/util.js')
var validator = require('../../../utils/validator.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        model: '',
        detail: {
            images: [],
        },
        images: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this
        let id = options.id || ''
        if (id != '') {
            http.request('product/readExtView', {id: id}, function(res) {
                that.setData({
                    detail: res
                })

                that.parseImages()
            })
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    bindInput(e) {
        let dataset = e.currentTarget.dataset;
        let value = e.detail.value;
        if (!value) {
            value = e.detail
        }
        let index = dataset.index;
        if (index != undefined) {
          this.data[dataset.obj][index][dataset.item] = value;
          this.setData({
            [dataset.obj]: this.data[dataset.obj]
          })
        } else {
          this.data[dataset.obj][dataset.item] = value;
          this.setData({
            [dataset.obj]: this.data[dataset.obj]
          })
        }
    },

    

    uploadImages(e) {
        let that = this
        
        let imgs = e.detail.file
        let data = {
          uploadDir: 'product'
        }

        for (var i = 0; i < imgs.length; i++) {
          http.uploadImage('common/uploadImage', imgs[i].url, data, function(res) {

            let img = {
              id: res.id,
              path: res.path
            };
            
            that.data.detail.images.push(img);
            that.setData({
                detail: that.data.detail
            })

            that.parseImages()
          });
        }
    },

    deleteImage(e) {
        let that = this
        let deleteImageIndex = e.detail.index
        that.data.detail.images.splice(deleteImageIndex, 1)
        that.setData({
            detail: that.data.detail
        })
        that.parseImages()
    },

    parseImages() {
        let that = this
        let tmpImages = []
        console.log(that.data.detail.images)
        for (var i = 0; i < that.data.detail.images.length; ++i) {
            let image = { 
                url: util.getImageUrl(that.data.detail.images[i].path) 
            }
            tmpImages.push(image)
        }

        that.setData({
            images: tmpImages
        })
    },

    submit() {
        let that = this
        if (validator.isEmptyObj(that.data.detail.name)) {
            util.toast('请输入产品名称', 'none', 2000)
            return
        }
        if (!validator.isPriceNumber(that.data.detail.price)) {
            util.toast('产品价格输入不正确', 'none', 2000)
            return
        }
        if (!validator.isInteger(that.data.detail.stock)) {
            util.toast('产品库存输入不正确', 'none', 2000)
            return
        }
        
        let requestParams = that.data.detail
        let imageFileIds = []
        for (var i = 0; i < that.data.detail.images.length; ++i) {
            imageFileIds.push(that.data.detail.images[i].id)
        }
        requestParams['imageFileIds'] = imageFileIds
        http.request('product/save', requestParams, function(res) {
            util.toast('保存成功', 'success', 2000, function() {
                that.refreshPrePage(that.data.detail.id ? 'update' : 'create');
                wx.navigateBack();
            })
        })
    },

    refreshPrePage(optionType) {
        let pages = getCurrentPages();
        if (pages.length > 1) {
          let prePage = pages[pages.length - 2];
            prePage.reloadAllListDatas();
        }
      },
})